package com.jxs.android.weixi;

import org.ksoap2.serialization.SoapObject;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.telecom.weixi.R;

public abstract class TableAbstractActivity extends AbstractListActivity {

	protected String title;
	protected int PAGE_SIZE = 5;
	protected int currentPage = 1; // 最后的可视项索引
	private static final int UPDATE_LIST_UI = 0x101;
	private static final int ERROR_MSG = 0x1013;
	private static final int TO_LOGIN = 0x1019;

	public int getLayout() {
		return R.layout.bu_common_with_list;
	}

	public void initContent() {

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		if (null != getIntent() && null != getIntent().getExtras()) {
			title = getIntent().getExtras().getString("title");
		}
		initTopBar(savedInstanceState, getLayout());
		initContent();
		loadingData(currentPage, true);
	}

	@Override
	protected void paser(SoapObject soapObject, Object... params) {
		if (null != soapObject) {
			String stauts = soapObject.getPropertySafelyAsString("status");
			SoapObject msgObjec = (SoapObject) soapObject.getProperty("msg");
			String message = msgObjec.getPropertySafelyAsString("message");
			if ("-2".equals(stauts)) {
				Message msg = new Message();
				msg.what = ERROR_MSG;
				msg.obj = message;
				handler.sendMessage(msg);
			} else if ("-1".equals(stauts)) {
				Message msg = new Message();
				msg.what = TO_LOGIN;
				msg.obj = message;
				handler.sendMessage(msg);
			} else if ("0".equals(stauts)) {

				Bundle bundle = new Bundle();

				if (soapObject.hasProperty("title")) {
					SoapObject titleObject = (SoapObject) soapObject.getProperty("title");
					bundle.putString("cust_manager_name", titleObject.getPropertySafelyAsString("cust_manager_name"));
					bundle.putString("cur_mon", titleObject.getPropertySafelyAsString("cur_mon"));
					bundle.putString("lst_mon", titleObject.getPropertySafelyAsString("lst_mon"));
					bundle.putString("l2st_mon", titleObject.getPropertySafelyAsString("l2st_mon"));

				}

				SoapObject taskBeanObject = (SoapObject) soapObject.getProperty("retMsg");
				Message msg = new Message();
				msg.what = UPDATE_LIST_UI;
				msg.setData(bundle);
				msg.obj = paserData(taskBeanObject, params);
				handler.sendMessage(msg);
			}
		}
	}

	Handler handler = new Handler() {

		@SuppressLint({ "HandlerLeak", "HandlerLeak" })
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == UPDATE_LIST_UI) {
				updateList(msg);
			} else if (msg.what == ERROR_MSG) {
				errorMsgPop(msg.obj.toString());
			} else if (msg.what == TO_LOGIN) {
				errorMsgToLogin(msg.obj.toString());
			}
		}
	};

	public abstract void loadingData(int currentPage, boolean showDialog);

	public abstract Object paserData(SoapObject retMsgObject, Object... params);

	public abstract void updateList(Message msg);

}
